<template>
  <div>
    <div class="background">
      <div class="login-form">
        <h2>登录</h2>
        <form action="/login" method="post" @submit.prevent="">
          <div class="form-group">
            <label for="username">用户名</label>
            <input
              type="text"
              id="username"
              name="username"
              required
              v-model="name"
            />
          </div>
          <div class="form-group">
            <label for="password">密码</label>
            <input
              type="password"
              id="password"
              name="password"
              required
              v-model="pwd"
            />
          </div>
          <div class="form-group">
            <label for="confirm-password">验证码</label>
            <input
              type="text"
              id="confirm-password"
              name="confirm-password"
              required
              v-model="code"
              style="width: 150px"
              ref="yanzheng"
            />&emsp;
            <button @click="sendCode" style="width: 100px">获取验证码</button>
          </div>
          <button type="submit" :plain="true" @click="login">登录</button>
          <div class="forgot-password">
            <router-link to="/zhuce">还未注册，点击注册</router-link>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "APP",
  data() {
    return {
      name: "",
      pwd: "",
      code: "",
      vercode: "", // 用于存储生成的验证码
    };
  },
  methods: {
    // 生成随机验证码
    sendCode() {
      // 生成4位随机验证码
      const randomCode = Math.random().toString(36).substr(2, 4).toUpperCase();
      this.vercode = randomCode; // 保存验证码
      alert(`验证码已发送，验证码为：${randomCode}`);
      this.code = ""; // 清空用户输入的验证码框
    },
    // 登录方法
    login() {
      // 从本地存储中获取用户信息
      const storedUserInfo = localStorage.getItem("userInfo");
      if (!storedUserInfo) {
        this.$message.error("未找到用户信息，请先注册！");
        return;
      }

      const { username, password } = JSON.parse(storedUserInfo);

      // 校验用户名、密码和验证码
      if (
        this.name === username &&
        this.pwd === password &&
        this.code === this.vercode
      ) {
        this.$message({
          message: "登录成功",
          type: "success",
        });
        const userInfo = localStorage.getItem("userInfo")
        localStorage.setItem("username", userInfo.username);
        localStorage.setItem("password", userInfo.password);
        this.$router.push("/host");
      } else {
        this.$message.error("登录失败,用户名或密码或验证码错误");
        // 清空用户输入的验证码框
        this.code = "";
      }
    },
  },
  created() {},
  mounted() {},
};
</script>

<style lang="less" scoped>
body,
html {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: Arial, sans-serif;
}

.background {
  background-image: url("./bgimg.webp"); /* 替换为你的背景图路径 */
  background-size: cover;
  background-position: center;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-form {
  background: rgba(255, 255, 255, 0.9); /* 半透明背景 */
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  width: 300px;
}

.login-form h2 {
  text-align: center;
  margin-bottom: 20px;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
}

.form-group input {
  width: 100%;
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

button {
  width: 100%;
  padding: 10px;
  border: none;
  background-color: #007bff;
  color: white;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background-color: #0056b3;
}

.forgot-password {
  text-align: right;
  margin-top: 10px;
}

.forgot-password a {
  color: #007bff;
  text-decoration: none;
}

.forgot-password a:hover {
  text-decoration: underline;
}
</style>
